package wh.coach._thread;

import java.util.Iterator;
import java.util.Random;
import java.util.Vector;

/**
 * @author hangwen
 * @date 2023/10/20
 */
public class ArrayListDemo {

    public static void main(String[] args) throws InterruptedException {
        //边读边写

//        var list = new ArrayList<Integer>();
        var list = new Vector<Integer>();

        Thread[] threads = new Thread[1];

        for (int i = 0; i < threads.length; i++) {
            threads[i] = new Thread(() -> {
                while (true) {
                    list.add(random(1, 100));
                }
            });
        }

        for (Thread thread : threads) {
            thread.start();
        }

        Thread.sleep(1000);

//        for (Integer integer : list) {
//            System.out.println(integer);
//        }

        final Iterator<Integer> iterator = list.iterator();
        while (iterator.hasNext()) {
            final Integer integer = iterator.next();

            System.out.println(integer);
        }
    }

    private static int random(int start, int end) {
        return start + new Random().nextInt(end - start);
    }
}
